﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using EOSCRM.Domain;
using EOSCRM.Util;

namespace EOSCRM.Dao
{
    public class _el_logTypeDao
    {
        readonly EventsLogDataContext _db;
        readonly string ConnectionString = "";
        public string SessionIdentify = "_EL_USER";
        private const string _dbName = "EventsLog";
        private const string _dbNameID = "Initial Catalog";

        public _el_logTypeDao()
        {
            var connStr = ConfigurationManager.AppSettings[Constants.SETTINGS_CONNECTION];
            var decryptString = connStr;  //EncryptUtil.Decrypt(connStr);

            var arrCS = decryptString.Split(';');

            for (var i = 0; i < arrCS.Length; i++)
            {
                var keyPair = arrCS[i].Split('=');
                if (keyPair.Count() == 2 && keyPair[0] == _dbNameID)
                    // EOSDN_EventsLog
                    arrCS[i] = string.Format("{0}={1}_{2}", keyPair[0], keyPair[1], _dbName);
            }
            ConnectionString = String.Join(";", arrCS);

            _db = new EventsLogDataContext(ConnectionString);
        }

        public el_logType Get(int logType) { return _db.el_logTypes.SingleOrDefault(w => w.logType == logType); }
        public List<el_logType> GetList() { return _db.el_logTypes.OrderBy(o => o.logName).ToList(); }
    }
}
